package com.gzc.just.play.last.war.centralserver.achievement;

/**
 * 玩家成就 - 记录玩家的成就进度和完成状态
 * 
 * @author AI编程助手
 * @version 1.0
 */
public class PlayerAchievement {
    
    private long playerId;
    private int achievementId;
    private AchievementConfig config;
    private int currentProgress;
    private boolean completed;
    private long completionTime;
    private boolean claimed;
    private long claimTime;
    
    /**
     * 构造函数
     * 
     * @param playerId 玩家ID
     * @param achievementId 成就ID
     * @param config 成就配置
     */
    public PlayerAchievement(long playerId, int achievementId, AchievementConfig config) {
        this.playerId = playerId;
        this.achievementId = achievementId;
        this.config = config;
        this.currentProgress = 0;
        this.completed = false;
        this.completionTime = 0;
        this.claimed = false;
        this.claimTime = 0;
    }
    
    /**
     * 增加进度
     * 
     * @param delta 增加的值
     * @return 是否完成了成就
     */
    public boolean addProgress(int delta) {
        if (completed) {
            return false;
        }
        
        currentProgress += delta;
        if (currentProgress >= config.getTargetValue()) {
            complete();
            return true;
        }
        return false;
    }
    
    /**
     * 设置进度
     * 
     * @param value 新的进度值
     * @return 是否完成了成就
     */
    public boolean setProgress(int value) {
        if (completed) {
            return false;
        }
        
        currentProgress = Math.max(value, currentProgress); // 只能增加，不能减少
        if (currentProgress >= config.getTargetValue()) {
            complete();
            return true;
        }
        return false;
    }
    
    /**
     * 标记成就为已完成
     */
    private void complete() {
        if (!completed) {
            completed = true;
            completionTime = System.currentTimeMillis();
            currentProgress = config.getTargetValue();
        }
    }
    
    /**
     * 领取成就奖励
     * 
     * @return 是否成功领取
     */
    public boolean claimReward() {
        if (!completed || claimed) {
            return false;
        }
        
        claimed = true;
        claimTime = System.currentTimeMillis();
        return true;
    }
    
    /**
     * 获取进度百分比（0-100）
     * 
     * @return 进度百分比
     */
    public int getProgressPercentage() {
        if (config.getTargetValue() == 0) {
            return 0;
        }
        return (int) ((currentProgress * 100) / config.getTargetValue());
    }
    
    /**
     * 是否可以领取奖励
     * 
     * @return 是否可以领取
     */
    public boolean canClaimReward() {
        return completed && !claimed;
    }
    
    // Getters and Setters
    
    public long getPlayerId() {
        return playerId;
    }
    
    public int getAchievementId() {
        return achievementId;
    }
    
    public AchievementConfig getConfig() {
        return config;
    }
    
    public int getCurrentProgress() {
        return currentProgress;
    }
    
    public boolean isCompleted() {
        return completed;
    }
    
    public long getCompletionTime() {
        return completionTime;
    }
    
    public boolean isClaimed() {
        return claimed;
    }
    
    public long getClaimTime() {
        return claimTime;
    }
    
    @Override
    public String toString() {
        return String.format("PlayerAchievement{playerId=%d, achievementId=%d, progress=%d/%d, completed=%s}",
                           playerId, achievementId, currentProgress, config.getTargetValue(), completed);
    }
}
